****************************************************
*This file reshapes total enrollment from the IPEDS*
****************************************************

foreach i in 1999 2006 2007 2008 2013 2014 2015 {
use "$clean_data_education/fallEnrollmentTotals.dta"
keep if year == `i'

* keep only useful informations 
keep if efalevel == 1 | efalevel == 2 | efalevel == 3 | efalevel == 4| ///
efalevel == 12 | efalevel == 16 | efalevel == 21 | efalevel == 22 | ///
efalevel == 23| efalevel == 24| efalevel == 41| efalevel == 42| ///
efalevel == 43| efalevel == 44

/*
efa		definition
1		All
2		All, und
3		All, und degree seeking
4		All, und degree seeking freshmen
12		All, grad
16		All, first prof
21		FT, all
22		FT, und
23		FT, und degree seeking
24 		FT, und degree seeking freshmen
41		PT, all
42		PT, und
43		PT, und degree seeking
44		PT, und degree seeking freshmen
*/


save "$clean_data_education/temporary_`i'.dta", replace
clear
}
***********************************************************************

foreach i in 2000 2001 {
use "$clean_data_education/fallEnrollmentTotals.dta"
keep if year == `i'

* keep only useful informations 
drop if line == 3 | line == 17
replace efalevel = line 
save "$clean_data_education/temporary_`i'.dta", replace
clear
}
***********************************************************************

foreach i in 1990 1991 1992 1993 1994 1995 {
use "$clean_data_education/fallEnrollmentTotals.dta"
keep if year == `i'

* keep only useful informations 
keep if line == 1 | line == 7| line == 8| line == 9| line == 11| line ==12| line == 14| ///
line == 15| line == 21| line == 22| line == 23| line == 25| line == 26| line == 28| line == 29

/*
line	definition
1		FT freshmen, und
7		FT und NON degr seeking
8		FT und
9		FT freshmen, firstprof
11		FT freshmen, grad
12		FT NON fresh, grad
14		FT total
15		PT freshmen, und
21		PT und NON degr seeking
22		PT und
23		PT freshmen, firstprof
25		PT freshmen, grad
26		PT NON fresh, grad
28		PT total
29 		All
*/

replace efalevel = line
save "$clean_data_education/temporary_`i'.dta", replace
clear
}
************************************************************************

*** reshape
foreach i in 1990 1991 1992 1993 1994 1995 1999 2000 2001 2006 2007 2008 2013 2014 2015 {
use "$clean_data_education/temporary_`i'.dta"
drop lstudy section line
reshape wide total men women, i(unitid) j(efalevel)
save "$clean_data_education/temporary_`i'.dta", replace
clear
}

*** make the variables omogeneous over time
foreach i in 1990 1991 1992 1993 1994 1995 {
cd "$clean_data_education/"
use "$clean_data_education/temporary_`i'.dta"

	foreach v in total men women {
		* replace missings with zeros
		foreach d in 1 7 8 9 11 12 14 15 21 22 23 25 26 28 29 {
		replace `v'`d' = 0  if mi(`v'`d') 
		}
	gen x_`v'1 = `v'29
	gen x_`v'2 = `v'8 + `v'22
	gen x_`v'4 = `v'1 + `v'15
	gen x_`v'12 = `v'11 + `v'12 + `v'25 + `v'26 +`v'9 + `v'23
	gen x_`v'21 = `v'8 + `v'14
	gen x_`v'22 = `v'8
	gen x_`v'23 = `v'8 - `v'7
	gen x_`v'24 = `v'1
	gen x_`v'41 = `v'22 + `v'28
	gen x_`v'42 = `v'22
	gen x_`v'43 = `v'22 - `v'21
	gen x_`v'44 = `v'15
	gen x_`v'3 = x_`v'23 + x_`v'43
	drop `v'*
	}
save "$clean_data_education/temporary_`i'.dta", replace
clear
}
****************************************

foreach i in 2000 2001 {
cd "$clean_data_education/"
use "$clean_data_education/temporary_`i'.dta"

	foreach v in total men women {
		* replace missings with zeros
		foreach d in 1 8 9 11 14 15 22 23 25 28 29 {
		replace `v'`d' = 0  if mi(`v'`d') 
		}
	gen x_`v'1 = `v'29
	gen x_`v'2 = `v'8 + `v'22
	gen x_`v'4 = `v'1 + `v'15
	gen x_`v'12 = `v'11 + `v'25 + `v'9 + `v'23
	gen x_`v'21 = `v'14
	gen x_`v'22 = `v'8
	*gen x_`v'23 = `v'8 - `v'7 // not available in 00-01
	gen x_`v'24 = `v'1
	gen x_`v'41 = `v'28
	gen x_`v'42 = `v'22
	*gen x_`v'43 = `v'22 - `v'21 // not available in 00-01
	gen x_`v'44 = `v'15
	*gen x_`v'3 = x_`v'23 + x_`v'43 // not available in 00-01
	drop `v'*
	}
save "$clean_data_education/temporary_`i'.dta", replace
clear
}

****************************************

foreach i in 1999 2006 2007 2008 2013 2014 2015 {
cd "$clean_data_education/"
use "$clean_data_education/temporary_`i'.dta"

	foreach v in total men women {
		foreach d in 1 2 3 4 12 21 22 23 24 41 42 43 44 {
		replace `v'`d' = 0  if mi(`v'`d') 
		rename `v'`d' x_`v'`d'
		}
		if `i' < 2013 {
		replace `v'16 = 0  if mi(`v'16) 
		replace x_`v'12 = x_`v'12 + `v'16 // sum first-professional to graduate
		drop `v'16
		}
	}
save "$clean_data_education/temporary_`i'.dta", replace
clear
}

*** label variables 
foreach i in 1990 1991 1992 1993 1994 1995 1999 2000 2001 2006 2007 2008 2013 2014 2015 {
cd "$clean_data_education/"
use "$clean_data_education/temporary_`i'.dta"
	
	foreach v in total men women {
	label variable x_`v'1 "All students"
	rename x_`v'1 `v'1
	label variable x_`v'2 "All undergraduate"
	rename x_`v'2 `v'2
	capture label variable x_`v'3 "undergraduate, degree seeking" // missing in 00-01
	capture rename x_`v'3 `v'3
	label variable x_`v'4 "freshmen"
	rename x_`v'4 `v'4
	label variable x_`v'12 "All graduate"
	rename x_`v'12 `v'12
	label variable x_`v'21 "All full-time"
	rename x_`v'21 `v'21
	label variable x_`v'22 "Undergraduate, full-time"
	rename x_`v'22 `v'22
	capture label variable x_`v'23 "Undergraduate, degree seeking full-time" // missing in 00-01
	capture rename x_`v'23 `v'23
	label variable x_`v'24 "freshmen, full-time"
	rename x_`v'24 `v'24
	label variable x_`v'41 "All part-time"
	rename x_`v'41 `v'41
	label variable x_`v'42 "Undergraduate, part-time"
	rename x_`v'42 `v'42
	capture label variable x_`v'43 "Undergraduate, degree seeking part-time" // missing in 00-01
	capture rename x_`v'43 `v'43
	label variable x_`v'44 "freshmen, part-time"
	rename x_`v'44 `v'44
	}

save "$clean_data_education/temporary_`i'.dta", replace
} 
****************************************

* append
use "$clean_data_education/temporary_1990.dta", clear
foreach i in 1991 1992 1993 1994 1995 1999 2000 2001 2006 2007 2008 2013 2014 2015 {
append using "$clean_data_education/temporary_`i'.dta"
rm "$clean_data_education/temporary_`i'.dta"
}
rm "$clean_data_education/temporary_1990.dta"

*** Build three year averages
* year group
gen yearGroup = 0
	replace yearGroup = 1990 if year <= 1991 
	replace yearGroup = 1994 if year >= 1992 & year <= 1994
	replace yearGroup = 2000 if year >= 1999 & year <= 2001
	replace yearGroup = 2007 if year >= 2006 & year <= 2008
	replace yearGroup = 2014 if year >= 2013
	drop if yearGroup == 0 // 1992
	
**********************************************************
* Run it all together, save labels and collapse
foreach v of var * {
local l`v' : variable label `v'
	if `"`l`v''"' == "" {
	local l`v' "`v'"
  	}
}
collapse (mean) total* men* women*, by(unitid yearGroup)

foreach v of var * {
label var `v' "`l`v''"
}
**********************************************************

save "$clean_data_education/fallEnrollmentTotalsReshaped.dta", replace



